import 'package:aichat_flutter_wallet/module/module_browser/pages/browser.dart';
import 'package:aichat_flutter_wallet/module/module_create_wallet/pages/create_mnemonic_page.dart';
import 'package:aichat_flutter_wallet/module/module_create_wallet/pages/import_wallet_page.dart';
import 'package:aichat_flutter_wallet/module/module_create_wallet/pages/mnemonic_tips_page.dart';
import 'package:aichat_flutter_wallet/module/module_create_wallet/pages/set_pass_page.dart';
import 'package:aichat_flutter_wallet/module/module_create_wallet/pages/verify_mnemonic_page.dart';

import 'package:aichat_flutter_wallet/module/module_dapp/pages/dapp_history_page.dart';

import 'package:aichat_flutter_wallet/module/module_dapp/pages/dapp_favorite_page.dart';

import 'package:aichat_flutter_wallet/module/module_dapp/pages/dapp_page.dart';
import 'package:aichat_flutter_wallet/module/module_news/pages/news_detail.dart';

import 'package:aichat_flutter_wallet/module/module_wallet/pages/wallet.dart';
import 'package:aichat_flutter_wallet/module/module_wallet_manage/pages/wallet_personal_center_page.dart';
import 'package:aichat_flutter_wallet/module/module_wallet/widgets/assets_component/select_wallet_list_page.dart';

import 'package:fluro/fluro.dart';

import 'package:flutter/cupertino.dart';

import '../../module/module_dapp/pages/dapp_hot_page.dart';
import '../../module/module_user_info/pages/fans.dart';
import '../../module/module_integral/pages/integral.dart';
import '../../module/module_wallet/pages/wallet_add_token.dart';
import '../../module/module_wallet/pages/wallet_custom_token.dart';
import '../../module/module_wallet/pages/wallet_detail.dart';
import '../../module/module_wallet/pages/wallet_export_privatekey.dart';

import 'package:aichat_flutter_wallet/module/module_trade/pages/trade_transfer.dart';

import '../../module/module_wallet/pages/wallet_mpc_securitymanager.dart';
import '../../module/module_wallet/pages/wallet_set_node.dart';
import 'root_page.dart';

/// 0是基础引擎（用于初始化数据库和一些基础通道传值） 1是dapp，2是wallet，3是钱包管理，4是创建钱包密码 5.积分入口

enum RootPageType {
  BaseInit,
  Dapp,
  Wallet,
  WalletManager,
  CreateWa,
  CreateWalletPWD,
  Integral,
  NewsDetail,
  Follow,
  Comment,
  Empty,
  Browser,
  Wcc,
  Fans
}

RootPageType rootPageType = RootPageType.Wallet;

class RoutesList {
  static Map<String, dynamic> flutters = {
    "/pages/activity/activityList": "/hot"
  };

  static bool isFlutterPage(page) {
    return flutters.containsKey(page);
  }

  static String FlutterVal(page) {
    return flutters[page];
  }
}

class Routes {
  static String rootRoute = '/'; // 定义路径的名字

  static String rootInitLoginRoute = '/init_login'; // 定义路径的名字

  static String rootSetPassRoute = '/set_pass'; // 定义路
  static String rootAssetsPage = '/wallet_assets'; // 定义路
  // 径的名字
  static String create_mnemonic = '/create_mnemonic'; // 定义路径的名字
  static String mnemonic_tips = '/mnemonic_tips';
  static String verifu_mnemonic = '/verifu_mnemonic';
  static String rootImportWallet = '/import_wallet'; // 定义路径的名字

  static String movieDetailRoute = '/movie'; // 定义路径的名字

  static String dAppRoute = '/dapp'; // 定义路径的名字

  static String dAppHistoryRoute = '/history'; // 定义路径的名字

  static String dAppHotRoute = '/hot'; // 定义路径的名字

  static String dAppFavoritesRoute = '/favorites'; // 定义路径的名字

  static String walletPage = '/home';

  static String browser = '/browser';

  static String search = '/search';

  static String wallet = '/wallet';
  static String selectChainList = '/select_chain_list';
  static String selectWalletList = '/select_wallet_list';
  static String walletAddToken = '/wallet_add_Token';
  static String walletCustomToken = '/wallet_custom_token';
  static String walletDetail = '/wallet_detail';
  static String walletExportPrivateKey = '/wallet_export_privatekey';
  static String walletSetNode = '/wallet_set_node';
  static String walletPersonal = '/wallet_personal';
  static String walletMPCSecurity = '/wallet_mpc_security';
  static String walletMPCRecast = '/wallet_mpc_recast';
  //
  static String checkCalendarPage = '/check_calendar_page';
  static String rankPage = '/rank';
  static String integralDetailPage = '/integral_detail_page';

  static String receive = '/receive';

  static String transfer = '/transfer';

  static String transactions = '/transactions';
  static String transactionDetails = '/transaction_details';
  static String news_detail = '/news_detail';

  static FluroRouter? router;

  static Future navigateTo(BuildContext context, String path,
      {Map<String, dynamic> params = const {},
      TransitionType transition = TransitionType.native,
      clearStack = false,
      replace = false}) {
    String query = "";
    int index = 0;
    for (var key in params.keys) {
      if (params[key] == null) {
        continue;
      }
      var value = Uri.encodeComponent(params[key]);
      if (index == 0) {
        query = "?";
      } else {
        query = query + "\&";
      }
      query += "$key=$value";
      index++;
    }
    path = path + query;
    return router!.navigateTo(context, path,
        transition: transition, replace: replace, clearStack: clearStack);
  }

  static void configureRoutes(FluroRouter router) {
    // 无法匹配路由时的处理

    // 默认路由

    router.define(
      rootImportWallet,
      handler: Handler(
        handlerFunc: (context, Map<String, dynamic> params) {
          ImportWalletPageType? type =
              params['type'][0] ?? ImportWalletPageType.CreateOrImportWallet;
          return ImportWalletPage(type: type!);
        },
      ),
    );

    // 设置密码 页面
    router.define(rootSetPassRoute, handler: Handler(
        handlerFunc: (BuildContext? context, Map<String, dynamic> params) {
      String type = params['type'][0] ?? '1';
      String privateKey = params['privateKey'][0] ?? '';
      return SetPassPage(
        type: type,
        privateKey: privateKey,
      );
    }));

    router.define(
      create_mnemonic,
      handler: Handler(
          handlerFunc: (BuildContext? context, Map<String, dynamic> params) {
        int wallet_id =
            params['wallet_id'][0] != null && params['wallet_id'][0].length > 0
                ? int.parse(params['wallet_id'][0])
                : 0;
        return CreateMnemonicPage(
          wallet_id: wallet_id,
        );
      }),
    );

    // 咨询详情
    router.define(
      news_detail,
      handler: Handler(
        handlerFunc: (context, parameters) => NewsDetailPage(),
      ),
    );

    router.define(
      dAppHistoryRoute,
      handler: Handler(
        handlerFunc: (context, parameters) => DAppHistoryPage(),
      ),
    );

    router.define(
      mnemonic_tips,
      handler: Handler(
        handlerFunc: (context, parameters) => const MnemonicTipsPage(),
      ),
    );

    router.define(
      verifu_mnemonic,
      handler: Handler(
        handlerFunc: (context, parameters) => const VerifyMnemonicPage(),
      ),
    );

    router.define(
      dAppHotRoute,
      handler: Handler(
        handlerFunc: (context, parameters) => const DAppHotPage(
          title: 'DAppHot',
        ),
      ),
    );

    router.define(
      dAppFavoritesRoute,
      handler: Handler(
        handlerFunc: (context, parameters) => const DAppFavoritePage(
          title: 'DAppFavorite',
        ),
      ),
    );

    router.define(
      browser, // 使用已经定义好的路径名
      handler: Handler(
        handlerFunc: (context, parameters) => BrowserPage(),
      ),
    );

    router.define(
      selectWalletList,
      handler: Handler(
        handlerFunc: (context, parameters) => SelectWalletListPage(),
      ),
    );

    router.define(
      walletAddToken,
      handler: Handler(
        handlerFunc: (context, parameters) => WalletAddToken(),
      ),
    );
    router.define(transfer, // 使用已经定义好的路径名
        handler:
            Handler(handlerFunc: (context, Map<String, dynamic> parameters) {
      String contract = parameters['contract'][0];
      String toAddress = parameters['toAddress'] ?? '';
      return TradeTransferPage(contract: contract, toAddress: toAddress);
    }));

    router.define(
      walletCustomToken,
      handler: Handler(
        handlerFunc: (context, parameters) => WalletCustomToken(),
      ),
    );

    router.define(
      walletDetail,
      handler: Handler(
        handlerFunc: (context, parameters) => WalletDetail(),
      ),
    );

    router.define(
      walletExportPrivateKey,
      handler: Handler(
        handlerFunc: (context, parameters) => WalletExportPrivateKey(),
      ),
    );

    router.define(
      walletSetNode,
      handler: Handler(
        handlerFunc: (context, parameters) => WalletSetNode(),
      ),
    );

    router.define(
      walletMPCSecurity,
      handler: Handler(
        handlerFunc: (context, parameters) => WalletMPCSecurityManager(
          pageType: MPCManagerPageType.Security,
        ),
      ),
    );

    router.define(
      walletMPCRecast,
      handler: Handler(
        handlerFunc: (context, parameters) => WalletMPCSecurityManager(
          pageType: MPCManagerPageType.Recast,
        ),
      ),
    );
    //  补充页面路由
    router.define(
      walletPersonal,
      handler: Handler(
        handlerFunc: (context, parameters) => WalletPersonalCenterPage(),
      ),
    );

    // "/"
    router.define(
      rootRoute, // 使用已经定义好的路径名
      handler: Handler(handlerFunc: (context, parameters) {
        //这里指定根页面。因为initialRoute虽然可以指定初始化展示的页面，
        //但是如果有页面返回根页面的话有可能会有问题（例：DAppHistoryPage的返回），所以应该要设置对应的根页面。
        return RootPage();
        // switch (rootPageType) {
        //   case RootPageType.BaseInit:
        //     return RootPage();
        //   case RootPageType.Fans:
        //     return FansPage();
        //   case RootPageType.Dapp:
        //     return DappPage();
        //   case RootPageType.Wallet:
        //     return WalletPage();
        //   case RootPageType.WalletManager:
        //     return WalletPersonalCenterPage();
        //   case RootPageType.Integral:
        //     return IntegralPage();
        //   case RootPageType.CreateWalletPWD:
        //     return SetPassPage(
        //       //创建钱包密码
        //       type: '4',
        //     );
        //   default:
        //     return RootPage();
        // }
      }),
    );
  }
}
